#include "function.h"
#include "scripts.h"
#include "sys/unistd.h"
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif

void print_message(const std::string& message)
{
	std::cout << message << std::endl;
}


void print_title(const std::string& name)
{
	print_message("  == SCH 快捷指令 " + name + " ==");
}

void print_line(const std::string& message)
{
	std::cout << message << std::endl;
}

void run_command(const std::string &command,const bool quite=false)
{
	std::string command_n;
	if (quite){
		command_n = command + ">/dev/zero 2>/dev/zero";
	}else{
		command_n = command;
	}
	const char* a = command_n.c_str();
	print_line("> " + command);
	int b = system(a);
}

bool sch_access(const char *path, int a){
        return access(path, a);
}

bool file_exist(const char *path)
{
	return sch_access(path,0)==0;
}

bool folder_exist(const char *path)
{
	return sch_access(path,0)!=0;
}

void sch_help(int argc,char * argv[])
{
	std::string arg2;
	if (argc >= 3){
		arg2 = argv[2];
	} else{
		arg2 = "";
	}
	if (arg2.empty()){
		print_title("帮助");
		print_message("sch [operate] [option]");
		print_line("-h --help");
		print_line("h help\t显示帮助信息");
		print_line("-b --build");
		print_line("b build\t指定编译类型");
		print_line("-d --default");
		print_line("d default\t设置默认执行脚本");
	}else if (arg2 == "-h" || arg2 == "--help" || arg2 == "h" || arg2 == "help"){
		print_title("编译帮助");
		print_message("sch -b [type]");
		print_line("hexo\t编译hexo博客");
		print_line("sch\t编译SCH博客");
	}else if (arg2 == "-i" || arg2 == "--install" || arg2 == "i" || arg2 == "install"){
		print_title("安装帮助");
		print_message("sch -i");
		print_line("将sch快捷指令安装到Linux系统中");
	}
}

void sch_build(int argc,char * argv[])
{
	std::string arg2;
	if (argc >= 3){
		arg2 = argv[2];
	} else{
		arg2 = "";
	}
	if (arg2 == "sch"){
        std::ifstream ifstream;
        ifstream.open(SCH_BUILD_SCH_BLOG,std::ios::in);
		print_title("编译");
		print_message("开始编译sch博客");
		if (folder_exist("public-tmp")){
			run_command("rm -rf public-tmp");
		}
		run_command("rm db.json");
		run_command("hexo generate --config _config-cn.yml",true);
		if (folder_exist("public-tmp/cn")){
			print_message("执行失败，编译有误");
			return;
		}
		if (folder_exist("public/cn")){
			run_command("rm -rf public/cn");
		}
		run_command("cp -r public-tmp/cn public/");
		run_command("rm -rf public-tmp/cn");
		run_command("rm db.json");
		run_command("hexo generate --config _config-tw.yml",true);
		if (folder_exist("public-tmp/tw")){
			print_message("执行失败，编译有误");
			return;
		}
		if (folder_exist("public/tw")){
			run_command("rm -rf public/tw");
		}
		run_command("cp -r public-tmp/tw public/");
		run_command("rm -rf public-tmp/tw");
		run_command("rm db.json");
		run_command("hexo generate --config _config-en.yml",true);
		if (folder_exist("public-tmp/en")){
			print_message("执行失败，编译有误");
			return;
		}
		if (folder_exist("public/en")){
			run_command("rm -rf public/en");
		}
		run_command("cp -r public-tmp/en public/");
		run_command("rm -rf public-tmp/en");
		run_command("rm -rf public-tmp");
		if(folder_exist("public/404")) {
			run_command("mkdir public/404");
		}
		run_command("cp index public/index.html");
		run_command("cp 404 public/404/index.html");
		run_command("cp public/cn/sitemap.txt public/");
		run_command("cp public/cn/sitemap.xml public/");
		run_command("cp public/cn/sitemap.txt public/sitemap-cn.txt");
		run_command("cp public/cn/sitemap.xml public/sitemap-cn.xml");
		run_command("cp public/en/sitemap.txt public/sitemap-en.txt");
		run_command("cp public/en/sitemap.xml public/sitemap-en.xml");
		run_command("cp public/tw/sitemap.txt public/sitemap-tw.txt");
		run_command("cp public/tw/sitemap.xml public/sitemap-tw.xml");
		run_command("cp verify/* public/");
		print_message("执行完成");

	}else if(arg2 == "sch-update"){
		print_title("编译");
		print_message("开始编译sch博客(快速)");
		run_command("hexo generate --config _config-cn.yml");
		run_command("hexo generate --config _config-en.yml");
		run_command("hexo generate --config _config-tw.yml");
		run_command("cp -r public-tmp/* public/");
		print_message("执行完成");
	}else if (arg2 == "hexo" || arg2 == "h"){
		print_title("编译");
		print_message("开始编译hexo博客");
		run_command("hexo clean");
		run_command("hexo generate");
		run_command("hexo deploy");
		print_message("执行完成");
	}
}

void sch_install() 
{
	print_title("安装");
	if (file_exist("main.cpp")){
		run_command("g++ main.cpp -o sch");
		if(file_exist("/bin/sch")){
			run_command("rm /bin/sch");
		}
		run_command("cp sch /bin/");
		run_command("chmod +x /bin/sch");
		run_command("rm sch");
		print_message("安装完成");
	} else{
		print_message("源文件不存在");
	}
}

void sch_default() 
{
	print_title("快速执行");
	int h,m,s;
	time_t now = time(NULL);
	tm *tm_t = localtime(&now);
	h = tm_t->tm_hour;
	m = tm_t->tm_min;
	s = tm_t->tm_sec;
	if (file_exist(".sch")) {
		std::ifstream file;
		file.open(".sch",std::ios::in);
		std::string data;
		std::string tmp;
		while (file >> tmp){
			data += tmp + " ";
		}
		if (data == "sch"){
			print_message("请不要套娃");
		} else {
			run_command(data);
			print_message("执行成功");
			now = time(NULL);
			tm_t = localtime(&now);
			h = tm_t->tm_hour - h;
			m = tm_t->tm_min - m;
			s = tm_t->tm_sec - s;
			int sum = h * 3600 + m * 60 + s;
			std::cout << "共耗时：" << sum << "秒";

		}
		file.close();
	} else{
		print_message("配置文件不存在");
	}
}


